#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"

/**
 * @brief 
 * 
 * @param sendbuf 
 * @param recvbuf 
 * @param count 
 * @param datatype 
 * @param op 
 * @param comm 
 * @return int 
 */
// int MPI_Exscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm);


int main(int argc, char** argv) {
    int rank, size;
    int sendbuf, recvbuf;

    // 初始化 MPI 环境
    MPI_Init(&argc, &argv);

    // 获取当前进程的 rank 和总的进程数
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    // 每个进程的发送缓冲区初始化为进程的 rank
    sendbuf = rank;

    // 执行 MPI_Exscan 操作
    MPI_Scan(&sendbuf, &recvbuf, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);

    // 打印结果
    // [INFO] Process 0: partial sum = 0
    // [INFO] Process 2: partial sum = 3
    // [INFO] Process 1: partial sum = 1
    // [INFO] Process 3: partial sum = 6
    INFO("Process %d: partial sum = %d\n", rank, recvbuf);
    MPI_Finalize();

    return 0;
}